import GameUiTools from "GameUiTools";
import GameConfig from "../GameConfig";
import GameTools from "GameTools";

cc.Class({
    extends: cc.Component,

    properties: {
        closeBtn: cc.Node,
        chooseBox: cc.Node,
        title: cc.Label,
        leftRole: cc.Node,
        centerRole: cc.Node,
        rightRole: cc.Node,
        ninjaSprite: cc.SpriteFrame,
        ninjaBlackSprite: cc.SpriteFrame,
        wizardSprite: cc.SpriteFrame,
        wizardBlackSprite: cc.SpriteFrame,
    },

    onLoad() {
        // 关闭按钮
        GameUiTools.setButtonClickEvents(this, this.closeBtn, "closeBtnFunc");
        // 监听滑动
        this._listenEvents();
    },

    start() {
        this.roleList = [
            {name: "ninja", chooseSprite: this.ninjaSprite, title: "忍者"},
            {name: "wizard", chooseSprite: this.wizardSprite, title: "巫师忍"},
        ];
        this.choosenIndex = GameTools.getItemByLocalStorage("CHOOSEN_ROLE_INDEX", 0);
        this.leftSprite = this.leftRole.getComponent(cc.Sprite);
        this.centerSprite = this.centerRole.getComponent(cc.Sprite);
        this.rightSprite = this.rightRole.getComponent(cc.Sprite);
        this._reposition();
    },

    closeBtnFunc: function() {
        // 选中角色
        GameConfig.CHOOSEN_ROLE_NAME = this.roleList[this.choosenIndex].name;
        GameTools.setItemByLocalStorage("CHOOSEN_ROLE_INDEX", this.choosenIndex);
        // 关闭浮层
        GameUiTools.unLoadingLayer(this.node);
    },

    _listenEvents() {
        var _self = this;
        var startX = null;
        this.chooseBox.on(cc.Node.EventType.TOUCH_START, function(event) {
            startX = event.getLocation().x;
        });

        this.chooseBox.on(cc.Node.EventType.TOUCH_END, function(event) {
            var moveX = event.getLocation().x - startX;
            if (moveX <= -100) {
                if (_self.choosenIndex < _self.roleList.length - 1) {
                    _self.choosenIndex ++;
                    _self._reposition();
                }
            } else if (moveX >= 100) {
                if (_self.choosenIndex > 0) {
                    _self.choosenIndex --;
                    _self._reposition();
                }
            }
        });
    },

    _reposition() {
        if (this.choosenIndex >= 0 && this.choosenIndex <= this.roleList.length - 1) {
            if (this.choosenIndex == 0) {
                this.leftSprite.spriteFrame = null;
            } else {
                this.leftSprite.spriteFrame = this.roleList[this.choosenIndex - 1].chooseSprite;
            }
            this.centerSprite.spriteFrame = this.roleList[this.choosenIndex].chooseSprite;
            this.title.string = this.roleList[this.choosenIndex].title.toString();
            if (this.choosenIndex == this.roleList.length - 1) {
                this.rightSprite.spriteFrame = null;
            } else {
                this.rightSprite.spriteFrame = this.roleList[this.choosenIndex + 1].chooseSprite;
            }
        }
    },

});
